package com.autologistics.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.autologistics.model.Goods;
import com.autologistics.util.HibernateSessionFactory;

import org.hibernate.Query;
import org.hibernate.Session;

public class GoodsDaoImpl implements GoodsDao{
	private HibernateTemplate goodsHibTemplate;

	
	
	public HibernateTemplate getGoodsHibTemplate() {
		return goodsHibTemplate;
	}



	public void setGoodsHibTemplate(HibernateTemplate goodsHibTemplate) {
		this.goodsHibTemplate = goodsHibTemplate;
	}



	public List<Goods> listAll(int userId){
		

		String hql="from Goods where userId=?";
		
		//		String hql="from Goods";
//		System.out.println("goodsDaoImpl "+hql);
		List<Goods> goodsList=this.goodsHibTemplate.find(hql,userId);
//		System.out.println("goodsList:size:"+goodsList.size()+goodsList.toString());
//		System.out.println(" "+goodsList.get(0).getName()+goodsList.get(0).getPrice()+goodsList.get(0).getGoodskind());
/*		if(!goodsList.isEmpty()){
			return goodsList;
		}else{
			return null;
		}*/
		return goodsList;
	}
	//下面是实现分页功能
	/**
	 * 得到总的商品数
	 * @param userId
	 * @return
	 */
	public int allRow(int userId){
		

		String hql="from Goods where userId=? order by id";
		int allRow =this.goodsHibTemplate.find(hql,userId).size();
		return allRow;
	}
	
	/**
	 * 得到某一页的数据
	 * @param offset
	 * @param length
	 * @param userId
	 * @return
	 */
	public List<Goods> queryForOnePage(final int offset, final int length,int userId){
		System.out.println("GoodsDaoimpl:  listAll");
		String hql="from Goods where userId="+userId+" order by id";
		Session session=goodsHibTemplate.getSessionFactory().openSession();
//		Session session =HibernateSessionFactory.getSession();
	    Query q = session.createQuery(hql);  
	    q.setFirstResult(offset);  
	    q.setMaxResults(length);  
	    List<Goods> goodsList = q.list();  
	  //  System.out.println(goodsList.size());  
	    session.close();  
	    return goodsList;  
	}
	
	
	//to save a new good. in special userId.
	public void addGood(Goods newGood){
		this.goodsHibTemplate.save(newGood);
		
	}
	
	//to get good through special id.
	public Goods queryGoodById(int id){
		String hql="from Goods where id=?";
		List<Goods> goodsList=this.goodsHibTemplate.find(hql, id);
		Goods goods=goodsList.get(0);
		if(goods!=null){
			return goods;
		}else{
			return null;
		}
	}
	
	//update object good.
	public void updateGood(Goods good){
		goodsHibTemplate.update(good);
	}
	
	//删除指定的商品
	public void deleteGood(int goodId){
		this.goodsHibTemplate.delete(this.queryGoodById(goodId));
	}
	
	public List<Goods> searchGoods(int userId,String name){
		try{
			String hql="from Goods where userId=? and name like '%"+name+"%'";
			List<Goods> goodsList=this.goodsHibTemplate.find(hql,userId);
			return goodsList;	
		}catch(RuntimeException e){
			throw e;
		}
	}
	public List<Goods> getGoodsByUserAndKind(int userId,int kind){
		String hql="from Goods where userId=? and kind=?";
		List<Goods> goodsList=this.goodsHibTemplate.find(hql,new Object[]{userId,kind});
		if (!goodsList.isEmpty()){
			return goodsList;
		}else{
			return null;
		}
	}
	public List<Goods> listAllGoods(){
		String hql="from Goods";
		List<Goods> goodsList=this.goodsHibTemplate.find(hql);
		if (!goodsList.isEmpty()){
			return goodsList;
		}else{
			return null;
		}
	}

}
